package com.ecnu.web.controller.tool;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ecnu.common.annotation.Log;
import com.ecnu.common.core.controller.BaseController;
import com.ecnu.common.core.domain.AjaxResult;
import com.ecnu.common.core.domain.model.LoginUser;
import com.ecnu.common.core.page.TableDataInfo;
import com.ecnu.common.enums.BusinessType;
import com.ecnu.system.domain.ProjectMessage;
import com.ecnu.system.domain.SysPrivacy;
import com.ecnu.system.domain.dto.MyMessageDTO;
import com.ecnu.system.domain.dto.SmtpEmailDTO;
import com.ecnu.system.service.IProjectMessageService;
import com.ecnu.system.service.ISysPrivacyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.lang.reflect.InvocationTargetException;

@Api(tags = "消息通知")
@RestController
@RequestMapping("/message")
@Validated
public class MessageController extends BaseController {
    @Autowired
    private IProjectMessageService projectMessageService;
    @Autowired
    private ISysPrivacyService sysPrivacyService;

    @Log(title = "我的消息", businessType = BusinessType.OTHER)
    @ApiOperation(value = "我的消息")
    //暂时这样展示，后续估计要改
    @PostMapping("/getMyMessage")
    public TableDataInfo getMyMessage(@Valid @RequestBody MyMessageDTO dto) throws InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException {
        LoginUser loginUser = getLoginUser();
        return projectMessageService.selectList(dto, loginUser);
    }

    /**
     * 消息提醒
     */
//    @Log(title = "我的消息--明细", businessType = BusinessType.OTHER)
    @ApiOperation(value = "消息类型的--明细")
    @GetMapping("/getMessage")
    public AjaxResult getMessage(@RequestParam("id") String id) {

        LoginUser loginUser = getLoginUser();
        ProjectMessage id1 = projectMessageService.getOne(new QueryWrapper<ProjectMessage>()
                .eq("id", id));
        if (id1 != null) {
            if (id1.getType().equals("1")) {
                new Thread(() -> {
                    ProjectMessage message = new ProjectMessage();
                    message.setId(id);
                    message.setStatus("1");
                    message.setUpdateUser(loginUser.getUserId().toString());
                    projectMessageService.updateById(message);
                }).start();
            }
        } else {
            id1 = new ProjectMessage();
        }
        return AjaxResult.success(id1);
    }

    /**
     * 隐私条款
     */
    @ApiOperation(value = "隐私条款--明细")
    @GetMapping("/getPrivacy")
    public AjaxResult getPrivacy() {
        SysPrivacy one = sysPrivacyService.getOne(null);
        if (one == null) {
            one = new SysPrivacy();
        }
        return AjaxResult.success(one);
    }

    @PostMapping("/privacyEdit")
//    @PreAuthorize("@ss.hasPermi('message:privacyEdit')")
    @ApiOperation(value = "隐私条款--编辑")
    public AjaxResult privacyEdit(@Valid @RequestBody SysPrivacy sysPrivacy) {
        boolean remove = sysPrivacyService.remove(null);
        boolean save = sysPrivacyService.save(sysPrivacy);
        if (save) {
            return AjaxResult.success("保存成功");
        }

        return AjaxResult.error("保存失败");

    }
}
